package com.techsoul.module.baoli.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.bean.BeanUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.baoli.enmus.FinCreditProveStatusEnum;
import com.techsoul.module.baoli.enmus.FinCustomerTypeEnum;
import com.techsoul.module.baoli.mapper.FinCustomerCreditApplicationMapper;
import com.techsoul.module.baoli.pojo.entity.FinCustomerCreditApplication;
import com.techsoul.module.baoli.pojo.param.FinCustomerCreditApplicationSaveParam;
import com.techsoul.module.baoli.pojo.param.FinCustomerCreditApplicationSearchParam;
import com.techsoul.module.baoli.pojo.vo.FinCustomerCreditApplicationVo;
import com.techsoul.module.baoli.service.IFinCustomerCreditApplicationService;
import com.techsoul.module.basic.mapper.BaoliCompanyMapper;
import com.techsoul.module.basic.mapper.CustomerMapper;
import com.techsoul.module.basic.mapper.StoreMapper;
import com.techsoul.module.basic.mapper.SupplierMapper;
import com.techsoul.module.basic.pojo.entity.Customer;
import com.techsoul.module.basic.pojo.entity.Store;
import com.techsoul.module.basic.pojo.entity.Supplier;
import com.techsoul.module.basic.pojo.vo.BaoliCompanyVo;
import com.techsoul.module.basic.pojo.vo.CustomerVo;
import com.techsoul.module.basic.pojo.vo.StoreVo;
import com.techsoul.module.basic.pojo.vo.SupplierVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户授信额度申请列 Service实现
 * 
 * @author liuen
 * @date 2022-03-28
 */
@Service
@Transactional(readOnly = true)
public class FinCustomerCreditApplicationServiceImpl implements IFinCustomerCreditApplicationService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private FinCustomerCreditApplicationMapper mapper;

    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private BaoliCompanyMapper baoliCompanyMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insert(FinCustomerCreditApplicationSaveParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        FinCustomerCreditApplication entity = new FinCustomerCreditApplication();
        BeanUtils.copyProperties(params, entity);
        if(user.getUserType()!=null&&user.getUserType()!=""){
            if(user.getUserType().equals("贸易商")){
                entity.setCustomerType(FinCustomerTypeEnum.trader.getValue());
                entity.setCustomerNo(user.getCustomerId());
                CustomerVo cust = this.customerMapper.findById(user.getCustomerId());
                entity.setCustomerName(cust.getCompanyName());
            }else if(user.getUserType().equals("买家客户")){
                entity.setCustomerType(FinCustomerTypeEnum.buyer.getValue());
                StoreVo cust = this.storeMapper.findById(user.getStoreId());
                entity.setCustomerName(cust.getStoreName());
                entity.setCustomerNo(user.getStoreId());
            }else if(user.getUserType().equals("供应商")){
                entity.setCustomerType(FinCustomerTypeEnum.supplier.getValue());
                SupplierVo cust = this.supplierMapper.findById(user.getSupplierId());
                entity.setCustomerName(cust.getSupplierName());
                entity.setCustomerNo(user.getSupplierId());
            }else if(user.getUserType().equals("平台公司")){
                entity.setCustomerType(FinCustomerTypeEnum.platform.getValue());
            }else{
                entity.setCustomerType(FinCustomerTypeEnum.platform.getValue());
            }
        }else{
            entity.setCustomerType(FinCustomerTypeEnum.platform.getValue());
        }
        entity.setSite(user.getSite());
        entity.setProveStatus(FinCreditProveStatusEnum.applied.getValue());
        entity.setId(idWorker.nextId() + "").preInsert(user.getId());
        this.mapper.insert(entity);
        return entity.getId();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {
        return this.mapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            return this.mapper.deleteBatchIds(idList);
        } else {
            return 0;
        }
    }

    /**
     * 修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer update(FinCustomerCreditApplicationSaveParam params) {
        FinCustomerCreditApplication entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entityFromDb);
        entityFromDb.preUpdate(user.getId());
        return this.mapper.updateById(entityFromDb);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public FinCustomerCreditApplicationVo findById(String id) {
        return this.mapper.findById(id);
    }
    @Transactional(readOnly = false)
    @Override
    public String prove(FinCustomerCreditApplicationSaveParam finCustomerCreditApplicationSaveParam) {
        FinCustomerCreditApplication entity =  this.mapper.selectById(finCustomerCreditApplicationSaveParam.getId());
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        if(finCustomerCreditApplicationSaveParam.getProveStatus().equals(FinCreditProveStatusEnum.rejected)){
            entity.setProveStatus(FinCreditProveStatusEnum.rejected.getValue());
            entity.preUpdate(user.getId());
             this.mapper.updateById(entity);
        }else{
            entity.setProveStatus(FinCreditProveStatusEnum.proved.getValue());
            entity.setBaoliSite(user.getBaoliCompanyId());
            if(entity.getCustomerType().equals(FinCustomerTypeEnum.buyer.getValue())){
                Store store = this.storeMapper.selectById(entity.getCustomerNo());
                BaoliCompanyVo baoliCompany = this.baoliCompanyMapper.findById(user.getBaoliCompanyId());
                store.setBaoliCompanyName(baoliCompany.getCompanyName());
                store.setBaoliCompanyNo(user.getBaoliCompanyId());
                if(finCustomerCreditApplicationSaveParam.getProvedCredit()!=null){
                    store.setCredit(finCustomerCreditApplicationSaveParam.getProvedCredit());
                    store.setCreditLeft(finCustomerCreditApplicationSaveParam.getProvedCredit());
                }

                store.preUpdate(user.getId());
                this.storeMapper.updateById(store);
            }else if(entity.getCustomerType().equals(FinCustomerTypeEnum.trader.getValue())){
                Customer customer = this.customerMapper.selectById(entity.getCustomerNo());
                BaoliCompanyVo baoliCompany = this.baoliCompanyMapper.findById(user.getBaoliCompanyId());
                customer.setBaoliCompanyName(baoliCompany.getCompanyName());
                customer.setBaoliCompanyNo(user.getBaoliCompanyId());
                if(finCustomerCreditApplicationSaveParam.getProvedCredit()!=null){
                    customer.setCredit(finCustomerCreditApplicationSaveParam.getProvedCredit());
                    customer.setCreditLeft(finCustomerCreditApplicationSaveParam.getProvedCredit());
                }

                customer.preUpdate(user.getId());
                this.customerMapper.updateById(customer);
            }
            entity.setProvedCredit(finCustomerCreditApplicationSaveParam.getProvedCredit());
            entity.preUpdate(user.getId());
            this.mapper.updateById(entity);
        }

        return entity.getId();
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<FinCustomerCreditApplicationVo> findAll(FinCustomerCreditApplicationSearchParam params) {
        return this.mapper.findAll(params);
    }

    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<FinCustomerCreditApplicationVo> findAll(FinCustomerCreditApplicationSearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAll(new Page<>(current, pageSize), params);
    }

}
